home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
7_10.lha
/
7_10
/
test5.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-08
|
2KB
|
95 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
*ident "@(#)ctrans:demo/task/shared.C 1.1.1.1" */
include <debug.h> /* DELETE */
**************************************************************************
Copyright (c) 1984 AT&T
All Rights Reserved
THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
The copyright notice above does not evidence any
actual or intended publication of such source code.
****************************************************************************/
include <process.h>
* non-trivial process example:
Make a set of SHARED processes
which pass an object round between themselves.
Each process but the last creates the next process.
Each process gets an object from the head of one queue, and puts
the object on the tail of another queue.
Each process quits after getting the object MAX_CYCLES times.
/
onst int NTASKS = 1;
onst int MAX_CYCLES = 5;
truct pc : process { // derive a class from process
queue_tail *t;
queue_head *h;
char *n;
pc(char*nn, queue_tail*nt, queue_head*nh) // process is not intended to be used directly
: (nn)
{
t = nt; h = nh; n = nn;
}
long main();
;
ong pc::main() // process body serves as
// "main" program for process
int cycles;
printf("new pc(%s)\n",n);
if (*n < 'a'+ NTASKS) {
char* c = new char[2]; c[0] = *n + 1; c[1] = '\0';
queue_tail* qt = new queue_tail;
pc* next = new pc(c, t, qt->head());
next->exec();
t = qt;
} else {
printf("%s: here we go\n", n);
t->put(new process_object);
}
for (cycles = 0; cycles < MAX_CYCLES; cycles++) {
process_object* p = h->get();
printf("process %s\n",n);
t->put(p);
}
printf("process %s: done.\n", n);
exit(0); // Always end process constructors with exit or return.
ain()
queue_head* hh = new queue_head;
queue_tail* t = hh->tail();
queue_head* h;
int cycles;
printf("main\n");
char* n = "a"; // make a one letter process name
h = new queue_head;
(new pc(n,t,h))->exec();
printf("main()'s loop\n");
t = h->tail();
for (cycles = 0; cycles < MAX_CYCLES; cycles++) {
process_object* p = hh->get();
printf("main process\n");
t->put(p);
}
printf("main process: done.\n", n);
main_process()->exit(0); // main is a process too; it must also
// end with resultis (to allow any
// remaining processes to run, otherwise
// the whole process would exit).
printf("should not get here\n");
return 0;